/**
 * Program  : DonwloadDialog.java
 * Author   : long
 * Create   : 2015��10��19�� ����11:30:26
 *
 * Copyright 2015 by Embedded Internet Solutions Inc.,
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Embedded Internet Solutions Inc.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with Embedded Internet Solutions Inc.
 *
 */
	
package cn.broadin.app.hotel.dialog;

import java.text.NumberFormat;

import android.app.Dialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.broadin.app.hotel.R;

public class DownLoadDialog extends Dialog{

	private ProgressBar mProgress;
	private boolean mHasStarted = false;
	private TextView mProgressTextView;
	private String mProgressNumberFormat;
	private NumberFormat mProgressPercentFormat;
	
	private int mMax;
	private int mCurrentVal;
	
	public DownLoadDialog(Context context){
		this(context, R.style.dialog_loading);
	}
	
	public DownLoadDialog(Context context, int theme) {
        super(context, theme);
        initView();
    }
	
	private void initView(){
		this.setContentView(R.layout.dialog_download);
		this.getWindow().getAttributes().gravity = Gravity.CENTER;
		this.getWindow().setWindowAnimations(R.style.dialogWindowAnim); //���ô��ڵ������� 
		
		mProgressTextView = (TextView) this.findViewById(R.id.download_number);
		mProgress = (ProgressBar) this.findViewById(R.id.download_progress);
		
		initFormats();
	}
	
	public void setMax(int max){
		mProgress.setMax(max);
		this.mMax = max;
	}
	
	public int getMax(){
		return mProgress.getMax();
	}
	
	public void setProgress(int progress){
		// ���͵�ǰ���
		mProgress.setProgress(progress);
		
		this.mCurrentVal = progress;
		
		// ����һ�ε�ǰ���
		double percent = (double) progress / (double) mMax;
        SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
        tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mProgressTextView.setText(tmp);
	}
	
	private void initFormats() {
	    mProgressNumberFormat = "%1.2fM/%2.2fM";
	    mProgressPercentFormat = NumberFormat.getPercentInstance();
	    mProgressPercentFormat.setMaximumFractionDigits(0);
	  }
	
	public static interface AddListener{
		public void onClick(int which);
	}
	
}

